package cn.edu.pet.servlet.user;

import cn.edu.pet.entity.AdoptRecord;
import cn.edu.pet.entity.User;
import cn.edu.pet.repo.AdoptRecordRepo;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet("/apply/adopt")
public class ApplyAdoptServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        String petId = req.getParameter("petId");
        User user = (User) req.getSession().getAttribute("user");
        AdoptRecord adoptRecord = new AdoptRecord();
        adoptRecord.setUserId(user.getId());
        adoptRecord.setPetId(Integer.valueOf(petId));
        adoptRecord.setStatus(0);
        adoptRecord.setCreatedTime(new Date());
        AdoptRecordRepo.getInstance().insertAdoptRecord(adoptRecord);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString("true");
        PrintWriter writer = resp.getWriter();
        writer.write(json);
    }
}
